// program/pages/rentCar/branch/index.js
const app = getApp();
const service = require('../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressType: 'From',
    // 选择的定位
    location: {
      name: '',
      address: '',
      latitude: '',
      longitude: ''
    },
    lastBranchList: [],
    branchList: []
  },

  // 获取当前定位
  getLocation() {
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        let params = {
          latitude: res.latitude,
          longitude: res.longitude
        }
        service.getConnect(service.api.getAddressByLongitudeAndLatitude, params).then(res => {
          that.setData({
            'location.name': res.data.formatted_addresses.recommend,
            'location.address': res.data.address,
            'location.latitude': res.data.location.lat,
            'location.longitude': res.data.location.lng
          })
          that.getRentCarBranchList();
          wx.hideLoading();
        })
      }
    })
  },

  // 选择定位
  toChooseLocation(e) {
    let type = e.currentTarget.dataset.type;
    console.log(e)
    let that = this;
    wx.chooseLocation({
      success: res => {
        console.log(res)
        that.setData({
          location: res
        });
        if (type === '0') {
          that.getRentCarBranchList();
        } else {
          // 获取当前页面js里面的pages里的所有信息
          let pages = getCurrentPages();
          // 获取上一页的数据
          let fatherPage = pages[pages.length - 2];
          if (that.data.addressType === 'From') {
            fatherPage.setData({
              'location': res.name,
              'address': res.address,
              'latitude': res.latitude,
              'longitude': res.longitude
            });
          }
          wx.navigateBack();
        }
      }
    })
  },

  // 获取网点列表
  getRentCarBranchList() {
    let that = this;
    let params = {
      latitude: this.data.location.latitude,
      longitude: this.data.location.longitude
    };
    service.getConnect(service.api.getRentCarBranchList, params).then(res => {
      that.setData({
        branchList: res.data
      })
      wx.hideLoading();
    })
  },

  // 获取最近网点列表
  getLastRentCarBranchList() {
    let that = this;
    service.getConnect(service.api.getLastChooseRentCarBranchList).then(res => {
      that.setData({
        lastBranchList: res.data
      })
      wx.hideLoading();
    })
  },

  // 选择网点
  bindSelectBranchItem(e) {
    let branchItem = e.currentTarget.dataset.item;
    console.log(branchItem)
    // 获取当前页面js里面的pages里的所有信息
    let pages = getCurrentPages();
    // 获取上一页的数据
    let fatherPage = pages[pages.length - 2];
    if (this.data.addressType === 'From') {
      fatherPage.setData({
        'location': branchItem.name,
        'address': branchItem.address,
        'latitude': branchItem.latitude,
        'longitude': branchItem.longitude
      });
    }
    if(this.data.addressType === 'To'){
      fatherPage.setData({
        'rentCar.addressToName': branchItem.name,
        'rentCar.addressTo': branchItem.address,
        'rentCar.addressToLatitude': branchItem.latitude,
        'rentCar.addressToLongitude': branchItem.longitude,
      });
    }
    wx.navigateBack();

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLastRentCarBranchList();
    this.getLocation();
    this.setData({
      addressType: options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})